/**
 * 作者: wgx
 * 日期: 2025-03-14 11:47:41
 * 备注: 用户子嗣教室表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.Common.PBUserSnatchTeam;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.commondata.skill.SkillConstant;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.logic.FightManager;
import com.motu.monstercity.server.game.logic.GameUser;
import com.motu.monstercity.server.game.logic.PartnerManager;
import com.motu.monstercity.server.game.logic.SkillManager;
import io.vertx.core.json.JsonArray;

import java.util.ArrayList;
import java.util.List;

public class UserSnatchTeam extends UserSnatchTeamModel {
    protected String test; // id

    public UserSnatchTeam() {
        super();
    }

    public String getTest() {
        return test;
    }

    // 给物资争夺快速战斗的队伍生成对手信息
    public void putEnemyId(UserInfo userInfo, int myRank) {
        long enemyIdNew = FightManager.getMatchEnemy(userInfo, myRank, super.getEnemyId());
        super.putEnemyId(enemyIdNew);
    }

    // 进行战斗 false 不发生战斗
    public boolean doFight(PBUserData.Builder pbUserData, UserInfo userInfo, UserPower userPower, List<UserPartner> partnerList, UserSnatchInfo userSnatchInfo) {
        List<UserSnatchPartner> userSnatchPartnerList = getUserSnatchPartnerList();
        int maxFightNum = FightManager.getMaxFightNum(userSnatchPartnerList);
        if (maxFightNum <= 0) {
            return false;// 没有战斗次数
        }
        int inspireValue = FightManager.countInspireValue(getInspireNum());// 鼓舞加成值4000，表示加成40%
        List<Long> myPowerList = getUserPartnerPowerList(userPower, inspireValue);// 该队伍的干员战力列表
        long enemyId = getEnemyId();
        List<Long> targetList = FightManager.getPowerList(enemyId);// 对方的战力列表
        int countKoNum = FightManager.countKoNum(myPowerList, targetList);
        int countFailNum = FightManager.countFailNum(myPowerList, targetList);
        if (countKoNum > 0) {
            userSnatchInfo.updateMaxKoNum(pbUserData, userInfo, countKoNum);// 物资争夺单场达到{0}连胜
        }

        // 更新对方的分数
        UserInfo userInfoEnemy = GameUser.getUserInfo(enemyId);
        UserSnatchScore userSnatchScoreTarget = FightManager.getUserSnatchScore(enemyId);
        int subValue = FightManager.getSubScore(countKoNum) * maxFightNum;// 对方扣除的物资数量
        userSnatchScoreTarget.subScore(userInfoEnemy, partnerList, subValue, LogType.FIGHT_SNATCH_FIGHT);
        userSnatchScoreTarget.update();

        int addValue = FightManager.getAddScore(countKoNum) * maxFightNum;// 我方增加的物资数量
        putKoNum(countKoNum);// 记录总的击败次数
        putFailNum(countFailNum);
        putFightNum(maxFightNum);
        putTransferItemNum(FightManager.takeTransferItemNum(countKoNum) * maxFightNum);
        putSnatchMoneyNum(FightManager.takeFightSnatchMoneyNum(countKoNum) * maxFightNum);
        putAddScore(addValue);
        putSubScore(subValue);
        // 生成对方的战报数据
        for (int i=0;i<maxFightNum;i++) {
            FightManager.createSnatchRecord(enemyId, userInfo, countKoNum, FightManager.getSubScore(countKoNum));
        }

        FightManager.updateSnatchPartnerFightNum(pbUserData, userSnatchPartnerList, 0, maxFightNum);//更新物资争夺的上阵干员的数据战斗次数
        FightManager.createSnatchFame(userInfo, enemyId, countKoNum);// 生成名人广播数据

        return true;
    }

    public List<UserSnatchPartner> getUserSnatchPartnerList() {
        List<UserSnatchPartner> result = new ArrayList<>();
        JsonArray jsonArray = getPartnerArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            int partnerId = jsonArray.getInteger(i);
            UserSnatchPartner userSnatchPartner = FightManager.getUserSnatchPartner(getUserId(), partnerId);
            result.add(userSnatchPartner);
        }
        return result;
    }

    /**
     * 获取该队伍的战力列表
     * @param userPower
     * @param addValue
     * @return
     */
    public List<Long> getUserPartnerPowerList(UserPower userPower, int addValue) {
        List<Long> result = new ArrayList<>();
        JsonArray jsonArray = getPartnerArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            int partnerId = jsonArray.getInteger(i);
            long myPartnerPower = GameUser.countPowerByPartner(userId, partnerId, null, userPower, addValue);// 该干员的战力
            result.add(myPartnerPower);
        }
        return result;
    }

    public PBUserSnatchTeam.Builder takeBuilder() {
        PBUserSnatchTeam.Builder sb = super.takeBuilder();
        UserInfo userInfo = GameUser.getUserInfo(getEnemyId());
        if (userInfo != null) {
            sb.setTargetIcon(userInfo.getIcon());
            sb.setTargetUserName(userInfo.getUserName());
        }
        
        return sb;
    }
}
